package cc.booku.owlbox.TestPage;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import cc.booku.owlbox.R;

class MyAdapter extends RecyclerView.Adapter<MyAdapter.ItemHolder> {

    private final Context context;
    private final List<Bean> data;

    public MyAdapter(Context context, List<Bean> data) {
        this.context = context;
        this.data = data;
    }

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        //创建
        //找到item
        View view = LayoutInflater.from(context).inflate(R.layout.test_item, viewGroup, false);
        //将布局设置给holder
        return new ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemHolder itemHolder, int position) {
        //将数据设置到item上
        Bean bean = data.get(position);
        itemHolder.text.setText(bean.getName());
        itemHolder.image.setImageResource(bean.getImage());
    }

    @Override
    public int getItemCount() {
        if (this.data != null) {
            return data.size();
        }
        return 0;
    }

    public class ItemHolder extends RecyclerView.ViewHolder {

        private final ImageView image;
        private final TextView text;

        public ItemHolder(@NonNull View itemView) {
            super(itemView);
            text = itemView.findViewById(R.id.text);
            image = itemView.findViewById(R.id.image);
        }
    }
}

